package com.ruoyi.shop.member.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.shop.member.mapper.ShopMemberAddressMapper;
import com.ruoyi.shop.common.constant.ShopConstants;
import com.ruoyi.shop.member.domain.ShopMemberAddress;
import com.ruoyi.shop.member.service.IShopMemberAddressService;

/**
 * 会员地址Service业务层处理
 * 
 * @author xuxiang
 */
@Service
public class ShopMemberAddressServiceImpl implements IShopMemberAddressService {
	@Autowired
	private ShopMemberAddressMapper shopMemberAddressMapper;

	/**
	 * 查询会员地址
	 * 
	 * @param addrId 会员地址主键
	 * @return 会员地址
	 */
	@Override
	public ShopMemberAddress selectAddressById(Long addrId) {
		return shopMemberAddressMapper.selectAddressById(addrId);
	}

	/**
	 * 查询会员地址
	 * 
	 * @param memberId 会员id
	 * @return 会员地址
	 */
	@Override
	public List<ShopMemberAddress> selectAddressByMemberId(Long memberId) {
		return shopMemberAddressMapper.selectAddressByMemberId(memberId);
	}

	/**
	 * 查询会员地址列表
	 * 
	 * @param ShopMemberAddress 会员地址
	 * @return 会员地址
	 */
	@Override
	public List<ShopMemberAddress> selectAddressList(ShopMemberAddress ShopMemberAddress) {
		return shopMemberAddressMapper.selectAddressList(ShopMemberAddress);
	}

	/**
	 * 新增会员地址
	 * 
	 * @param ShopMemberAddress 会员地址
	 * @return 结果
	 */
	@Override
	public int insertAddress(ShopMemberAddress ShopMemberAddress) {
		if (ShopConstants.YES.equals(ShopMemberAddress.getIsDefault())) {
			cancelDefaultAddress(ShopMemberAddress.getMemberId());
		}
		ShopMemberAddress.setCreateTime(DateUtils.getNowDate());
		ShopMemberAddress.setUpdateTime(DateUtils.getNowDate());
		return shopMemberAddressMapper.insertAddress(ShopMemberAddress);
	}

	/**
	 * 修改会员地址
	 * 
	 * @param ShopMemberAddress 会员地址
	 * @return 结果
	 */
	@Override
	public int updateAddress(ShopMemberAddress ShopMemberAddress) {
		if (ShopConstants.YES.equals(ShopMemberAddress.getIsDefault())) {
			cancelDefaultAddress(ShopMemberAddress.getMemberId());
		}
		ShopMemberAddress.setUpdateTime(DateUtils.getNowDate());
		return shopMemberAddressMapper.updateAddress(ShopMemberAddress);
	}

	/**
	 * 删除会员地址信息
	 * 
	 * @param addrId 会员地址主键
	 * @return 结果
	 */
	@Override
	public int deleteAddressById(Long addrId) {
		return shopMemberAddressMapper.deleteAddressById(addrId);
	}

	/**
	 * 获取会员默认地址
	 * 
	 * @param memberId
	 */
	@Override
	public ShopMemberAddress getDefaultAddress(Long memberId) {
		return shopMemberAddressMapper.getDefaultAddress(memberId);
	}

	/**
	 * 取消会员默认地址
	 * 
	 * @param memberId 会员id
	 * @return
	 */
	@Override
	public void cancelDefaultAddress(Long memberId) {
		shopMemberAddressMapper.cancelDefaultAddress(memberId);
	}

}
